<template>
  <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
    <el-form :model="form" :rules="rules">
      <el-form-item label="角色名字" :label-width="formLabelWidth">
        <el-input v-model="form.roleName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="角色描述"  :label-width="formLabelWidth">
        <el-input v-model="form.roleDesc" prop="msg" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="dialogFormVisible = false;submit()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator'
import { editRole } from '@/api/role'
import { MessageType } from 'element-ui/types/message'
export interface Role {
  id:number
  roleName:string,
  roleDesc?:string
}
@Component
export default class RolesEdit extends Vue {
  private form:Role={
    id: 0,
    roleName: '',
    roleDesc: ''
  }

  private dialogFormVisible=false
  private formLabelWidth='120'
  private rules:unknown={
    msg: [{
      validator (_:unknown, value:string, cb:(a?:Error)=>never|void) {
        if (value === '') {
          cb(new Error('不能为空'))
        } else if (value.length < 2 || value.length > 10) {
          cb(new Error('长度为3-20'))
        } else cb()
      },
      trigger: 'change'
    }]
  }

  isOpen (data:Exclude<Role, 'id'>) {
    this.form = data
    this.dialogFormVisible = true
  }

  async submit () {
    console.log(this.form)
    const { meta } = await editRole(this.form)
    let type:MessageType
    if (meta.status >= 200 && meta.status < 300) {
      type = 'success'
    } else type = 'error'

    this.$message({
      message: meta.msg,
      type
    })
  }
}
</script>

<style scoped>

</style>
